package com.zengjianjun.es.demo.highlevelclient.param;

import com.zengjianjun.es.demo.highlevelclient.options.ConditionOption;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * <p>es字段包装类</p>
 * <p>
 * <hr/>
 *
 * <p>FieldValue用于在查询 elasticsearch 构建 EsQueryParam 时，传入参数。</p>
 *
 * <p>例如：</p>
 * <p><blockquote><pre class="code">
 *          EsQueryParam esQueryParam = new EsXxxQueryParam()
 *                 .aField(new FieldValue&lt;String&gt;().value(aField).conditionOption(ConditionOption.equal))
 *                 .bField(new FieldValue&lt;Integer&gt;().value(bField).conditionOption(ConditionOption.or))
 *                 .cField(new FieldValue&lt;List&lt;String&gt;&gt;().value(cField).conditionOption(ConditionOption.not_equal))
 * </pre></blockquote></p>
 * <ol type="disc">
 * <li/>必须指明字段类型。
 * <li/>必须指明条件类型 ConditionOption。
 * </ol>
 * <p>
 * <hr/>
 *
 * @author zengjianjun
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class FieldValue<V> {

    private V value;

    private ConditionOption conditionOption;

    public FieldValue<V> value(V value) {
        this.value = value;
        return this;
    }

    public FieldValue<V> conditionOption(ConditionOption conditionOption) {
        this.conditionOption = conditionOption;
        return this;
    }

    public static <V> FieldValueBuilder<V> builder() {
        return new FieldValueBuilder<V>();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FieldValueBuilder<V> {

        private V value;

        private ConditionOption conditionOption;

        public FieldValueBuilder<V> value(V value) {
            this.value = value;
            return this;
        }

        public FieldValueBuilder<V> conditionOption(ConditionOption conditionOption) {
            this.conditionOption = conditionOption;
            return this;
        }

        public FieldValue<V> build() {
            return new FieldValue<>(value, conditionOption);
        }
    }
}
